package com.fjnu.controller;

import com.fjnu.common.ResponseResult;
import com.fjnu.entity.Tag;
import com.fjnu.dao.TagDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签管理控制器
 */
@RestController
@RequestMapping("/blog-api/v0.1/tag")
public class TagController {

    @Autowired
    private TagDao tagDao;

    /**
     * 罗列所有的标签
     * GET: /blog-api/v0.1/tag/list
     */
    @GetMapping("/list")
    public ResponseResult<List<Tag>> list() {
        try {
            List<Tag> tags = tagDao.selectAll();
            return ResponseResult.success(tags);
        } catch (Exception e) {
            return ResponseResult.error("查询标签列表失败: " + e.getMessage());
        }
    }

    /**
     * 新增标签
     * POST: /blog-api/v0.1/tag/add
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Tag tag) {
        try {
            int result = tagDao.insert(tag);
            if (result > 0) {
                return ResponseResult.success("标签新增成功");
            } else {
                return ResponseResult.error("标签新增失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("新增标签失败: " + e.getMessage());
        }
    }

    /**
     * 删除指定标签
     * GET: /blog-api/v0.1/tag/delete
     */
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam String tagId) {
        try {
            int result = tagDao.delete(Integer.valueOf(tagId));
            if (result > 0) {
                return ResponseResult.success("标签删除成功");
            } else {
                return ResponseResult.error("标签删除失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("删除标签失败: " + e.getMessage());
        }
    }
}